package dletc.lock;

import java.util.ArrayList;

//ArrayList是非线程安全的
public class unsafeList {
    public static void main(String[] args) {
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                //添加同步代码块
                synchronized (list){
                    list.add(Thread.currentThread().getName()); //添加10000个线程
                }
            }).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("list的总容量为:" + list.size());
    }
}
